package com.framework.utils.StarPos;

import com.alibaba.fastjson.JSONObject;
import com.framework.utils.MD5Util;
import com.framework.utils.MapObjUtils;
import com.framework.utils.StarPos.CustomerQuickRegistBean.CustomerSignInfo;
import com.framework.utils.StarPos.CustomerQuickRegistBean.CustomerUnsignBean;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.*;

/**
 * 快速进件接口
 */
public class CustomerQuickRegist {

    //private final static String BaseUrl = "http://sandbox.starpos.com.cn/emercapp";
    private final static String BaseUrl="https://gateway.starpos.com.cn/emercapp";
    //private final static String orgNo = "518";
    private final static String orgNo="1467";

    //private final static String key = "9773BCF5BAC01078C9479E67919157B8";
    private final static String key="6928A794B9F3ACFF82E9C0FCE18C4449";

    /**
     * 星pos商户进件
     * @param info
     * @param unsignBean
     * @return
     */
    public static String customerQuickRegist(CustomerSignInfo info, CustomerUnsignBean unsignBean){
        info.setOrgNo(orgNo);
        info.setServiceId("6060601");
        SortedMap<String ,Object> map=new TreeMap<>();

        String body = null;
        try {
            Map<String, Object> registerMap = MapObjUtils.objToMap(info);
            map.putAll(registerMap);

            String waitSign="";
            Set<String> keys = map.keySet();

            Iterator<String> it = keys.iterator();
            System.out.print("验签字段:");
            while (it.hasNext()){
                    String keytemp=it.next();
                    if(map.get(keytemp)!=null){
                        waitSign+=String.valueOf(map.get(keytemp));
                        System.out.print(keytemp+",");
                    }
            }
            System.out.println();
            waitSign+=key;
            System.out.print("验签内容:  ");
            System.out.println(waitSign);
            String signValue= MD5Util.MD52(waitSign);
            System.out.println(signValue);

            HttpEntity entity1;
            HttpClient httpClient = new DefaultHttpClient();
            map.putAll(MapObjUtils.objToMap(unsignBean));
            map.put("signValue",signValue);
            String content;
            content=JSONObject.toJSONString(map);
            HttpPost post = new HttpPost(BaseUrl);
            //utf-8项目发送gbk中文 我不知道为啥这样写，不过没得错
            StringEntity entity = new StringEntity(content,ContentType.create("application/json","gbk"));
            post.setEntity(entity);

            org.apache.http.HttpResponse httpresponse = httpClient.execute(post);

            entity1 = httpresponse.getEntity();
            body = EntityUtils.toString(entity1, "gbk");
            System.out.println(body);


        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;

    }
}
